package com.realtyManage.springboot.Dao;


import com.realtyManage.springboot.Model.Do.Area;
import com.realtyManage.springboot.Model.Do.ProjectImg;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 项目图片
 *
 * @author 杭州素色网络科技有限公司
 * @since 2019-10-28
 */

@Repository
public interface ProjectImgRepository extends JpaRepository<ProjectImg, Integer>, JpaSpecificationExecutor<ProjectImg> {

    @Modifying
    @Transactional
    @Query("update ProjectImg a set " +
            "a.img = CASE WHEN :#{#projectImg.img} IS NULL THEN a.img ELSE :#{#projectImg.img} END ," +
            "a.type = CASE WHEN :#{#projectImg.type} IS NULL THEN a.type ELSE :#{#projectImg.type} END " +
            "where a.type = :#{#projectImg.type}")
    int update(@Param("projectImg") ProjectImg projectImg);

    @Query(value = "SELECT LENGTH(img) - LENGTH( REPLACE(img,':','')) num FROM project_img WHERE type != 5",nativeQuery = true)
    List getCountImg();


    @Query(value = "SELECT * FROM project_img where type = ?1",nativeQuery = true)
    List getImgByType(Integer type);

}
